//task_material.js
const util = require("../../../utils/util.js");
const app = getApp()

Page({
  data: {
    taskViewId: '',
    selectedTask_type: "",
    // 任务字典
    taskDic: {},
    // 勘测字典
    surveyDic: {},
    // 房间物料展示数组
    house_materiel_array: [],
    // 安装位置图片
    surImages: [],
    // 物料数组
    materielNameArr: []
  },
  onLoad(options) {
    this.$wuxToast = app.wux(this).$wuxToast;
    this.setData({
      taskViewId: options.id,
      selectedTask_type: options.taskStatusText
    })
    this.getMaterielList();
    this.refreshNewData();
  },
  //刷新数据
  refreshNewData: function () {
    //加载提示框
    util.showLoading();
    var that = this;
    var urlStr = app.globalData.BaseURL + '/api/task/report';
    // 请求后台预约时间
    wx.request({
      method: "GET",
      url: urlStr,
      data: { viewId: that.data.taskViewId },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'adminUserViewId': app.globalData.adminUserViewId,
        'token': app.globalData.token
      },
      success: function (res) {
        util.hideToast();
        var code = res.data.code;
        console.log("data===" + res.data.data)
        var materialPreStr = JSON.parse(res.data.data.surveyReport.materielRequirement);
        if (code === 200) {

          var surveyImagesUrls = [];
          res.data.data.surveyImage.forEach(function (e) {
            surveyImagesUrls.push(e.url);
          })
          
          // 属性赋值
          that.setData({
            taskDic: res.data.data.task,
            surveyDic: res.data.data.surveyReport,
            house_materiel_array: materialPreStr,
            surImages: surveyImagesUrls,
          });
        }
      }, complete: function () {
        util.hideToast();
      }
    })
  },
  // 查看勘测表
  showKanceImage: function () {
    var that = this;
    wx.previewImage({
      current: that.data.surveyDic.url, // 当前显示图片的http链接
      urls: [that.data.surveyDic.url] // 需要预览的图片http链接列表
    })
  },
  // 安装位置图片查看
  showImage: function (event) {
    var index = event.currentTarget.dataset.index;
    var that = this;
    var currentUrl = that.data.surImages[index];

    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: that.data.surImages // 需要预览的图片http链接列表
    })
  },
  //加载物料列表
  getMaterielList: function () {
    var that = this;
    var urlStr = '/api/task/getMaterielList';
    var parameters = "";
    util.request(urlStr, parameters, function (res) {
      console.log("data===" + res);
      var materielArray = res.data.data;
      console.log("materielArray===" + materielArray);
      that.setData({
        materielNameArr: materielArray
      });
    })
  }, complete: function () {
    util.hideToast();
  }
})

